package com.un.ebs.context.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.un.ebs.context.service.SystemDbService;
import com.un.ebs.core.exception.BusinessException;
import com.un.ebs.core.metadata.EnumInfo;
import com.un.ebs.core.metadata.FunctionInfo;
import com.un.ebs.core.metadata.ServiceInfo;
import com.un.ebs.core.util.PackageUtil;
import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;


@Api(value = "context", tags = {"这是一个新的属性"})
@RestController
@RequestMapping("context")
public class ContextController {

    @Resource
    private SystemDbService dbService;

    @ApiOperation("枚举定义 : IEnum")
    @RequestMapping(value = "enums", method = {RequestMethod.GET})
    public WebResult<Map<String, List<EnumInfo>>> queryEnums() {
        return WebUtils.success(PackageUtil.getEnums(""));
    }

    @ApiOperation("枚举定义 : IEnum")
    @RequestMapping(value = "enums/{enumTypeName}", method = {RequestMethod.GET})
    public WebResult<List<EnumInfo>> queryEnums(@PathVariable String enumTypeName) {
        try {
            Class cls = Class.forName(enumTypeName);
            return WebUtils.success(PackageUtil.getEnums(cls));
        } catch (ClassNotFoundException e) {
            return WebUtils.error(new BusinessException("指定的枚举类型不存在。"));
        }
    }

    @ApiOperation("服务定义 : @RestController")
    @GetMapping("services")
    public WebResult<List<ServiceInfo>> queryServices() {
        return WebUtils.success(PackageUtil.getControllers(null));
    }

    @ApiOperation("接口定义 : @RequestMapping,@GetMapping,@PostMapping...")
    @GetMapping(value = "function")
    public WebResult<List<FunctionInfo>> queryFunctions(@RequestParam String serviceName) {
        return WebUtils.success(PackageUtil.getFunctions(serviceName));
    }

    @PostMapping(value = "query/{objName}")
    @ApiOperation("动态查询")
    public WebResult<IPage<LinkedHashMap<String, Object>>> query(@PathVariable String objName,
                                                                 @RequestParam(required = false) String sortName,
                                                                 @RequestParam(required = false) Integer pageIndex,
                                                                 @RequestParam(required = false) Integer pageSize,
                                                                 @RequestBody(required = false) Map<String, Object> args) {
        if (pageIndex == null) {
            pageIndex = 1;
        }
        if (pageSize == null) {
            pageSize = 50;
        }
        return WebUtils.success(dbService.execQuery(objName, sortName, args, pageIndex, pageSize));
    }

    @PostMapping(value = "queryView/{objName}")
    @ApiOperation("动态查询")
    public WebResult<IPage<LinkedHashMap<String, Object>>> queryView(@PathVariable String objName,
                                                                     @RequestParam(required = false) String sortName,
                                                                     @RequestParam(required = false) Integer pageIndex,
                                                                     @RequestParam(required = false) Integer pageSize,
                                                                     @RequestBody(required = false) String args) {
        if (pageIndex == null) {
            pageIndex = 1;
        }
        if (pageSize == null) {
            pageSize = 50;
        }
        return WebUtils.success(dbService.execQuery(objName, sortName, args, pageIndex, pageSize));
    }


}
